package client.comm.baoding.ui

import android.view.View
import androidx.lifecycle.lifecycleScope
import androidx.paging.LoadState
import client.comm.commlib.base.PagerFooterAdapter
import client.comm.commlib.comm_ui.draw.SpaceItemDecoration
import client.comm.commlib.toast.toast
import client.comm.commlib.utils.StoreUtil
import client.comm.commlib.widget.EmptyLayout
import client.comm.baoding.R
import client.comm.baoding.adapter.DaySignAdapter
import client.comm.baoding.api.bean.UserInfo
import client.comm.baoding.databinding.ActivityDaysignBinding
import client.comm.baoding.ui.vm.DaySignViewModel
import kotlinx.coroutines.flow.collect
import kotlinx.coroutines.launch
import client.comm.baoding.BaseMiddleActivity

class DaySignActivity : BaseMiddleActivity<DaySignViewModel, ActivityDaysignBinding>() {

    val user: UserInfo? by lazy {
        StoreUtil.getBean<UserInfo>(this@DaySignActivity).apply {
            this?.let {
                viewModel.sign.value = it.sign_days
                viewModel.isSign.value = it.today_sign == 1

                binding.signBtn.text = if (it.today_sign == 1) "已签到" else "签到"
            }
        }
    }

    val adapter: DaySignAdapter by lazy {
        DaySignAdapter(this).apply {
            addLoadStateListener {
                when (it.refresh) {
                    is LoadState.NotLoading -> {
                        if (itemCount == 0) {
                            binding.emptyLayout.errorType = EmptyLayout.NODATA
                        } else {
                            binding.emptyLayout.visibility = View.GONE
                        }
                    }

                    is LoadState.Loading -> {
                        if (itemCount == 0) {
                            binding.emptyLayout.errorType = EmptyLayout.NETWORK_LOADING
                        }
                    }

                    is LoadState.Error -> {
                        if (itemCount == 0)
                            binding.emptyLayout.errorType = EmptyLayout.NETWORK_ERROR
                        toast((it.refresh as LoadState.Error).error.message)
                    }
                }
                when (it.append) {
                    is LoadState.Error -> {
                        viewModel.isLoadMore.value = true
                    }

                    else -> {}
                }
            }
        }
    }

    //所有，释放中，冻结中，
    override fun getLayout(): Int {
        return R.layout.activity_daysign
    }

    override fun initViewModel(): DaySignViewModel = createViewModel()

    override fun initView() {
        binding.event = this
        binding.vm = viewModel
        binding.user = user
        binding.recyclerView.adapter = adapter.withLoadStateFooter(
            footer = PagerFooterAdapter(),
        )
        binding.recyclerView.addItemDecoration(SpaceItemDecoration(10))

        binding.recyclerView.onLoadMore = {
            if (viewModel.isLoadMore.value == true)
                adapter.retry()
            viewModel.isLoadMore.value = false

        }

        lifecycleScope.launch {
            viewModel.pagingDataUser().collect {
                adapter.submitData(it)
            }
        }
        binding.emptyLayout.setOnClickListener {
            adapter.refresh()
        }
    }

    override fun observerUI() {

        viewModel.callUser.observe(this, {
            it?.let {
                if (it.isSuccess) {

                    it.data?.let {
                        viewModel.sign.value = it.sign_days
                        viewModel.isSign.value = it.today_sign == 1
                        binding.signBtn.text = if (it.today_sign == 1) "已签到" else "签到"

                        binding.user = it
                    }
                }
            }
        })
        viewModel.startCall.observe(this, {
            it?.let {
                if (it.isSuccess) {
                    viewModel.isSign.value = true
                    binding.signBtn.text = "已签到"

                    adapter.refresh()
                    viewModel.userInfo()
                } else {
                    viewModel.isSign.value = false
                }
            }
        })
        viewModel.sign.observe(this, {

            when (it) {
                1 -> {
                    binding.oneIv.isSelected = true
                    binding.twoIv.isSelected = false
                    binding.threeIv.isSelected = false
                    binding.fourIv.isSelected = false
                    binding.fiveIv.isSelected = false
                    binding.sixIv.isSelected = false
                    binding.sevenIv.isSelected = false

                    binding.oneFirstView.isSelected = true
                    binding.twoFirstView.isSelected = false
                    binding.threeFirstView.isSelected = false
                    binding.fourFirstView.isSelected = false
                    binding.fiveFirstView.isSelected = false
                    binding.sixFirstView.isSelected = false
                    binding.sevenFirstView.isSelected = false

                    binding.oneSecondView.isSelected = false
                    binding.twoSecondView.isSelected = false
                    binding.threeSecondView.isSelected = false
                    binding.fourSecondView.isSelected = false
                    binding.fiveSecondView.isSelected = false
                    binding.sixSecondView.isSelected = false
                    binding.sevenSecondView.isSelected = false

                    binding.oneSign.visibility = View.VISIBLE
                    binding.twoSign.visibility = View.INVISIBLE
                    binding.threeSign.visibility = View.INVISIBLE
                    binding.fourSign.visibility = View.INVISIBLE
                    binding.fiveSign.visibility = View.INVISIBLE
                    binding.sixSign.visibility = View.INVISIBLE
                    binding.sevenSign.visibility = View.INVISIBLE
                }

                2 -> {
                    binding.oneIv.isSelected = true
                    binding.twoIv.isSelected = true
                    binding.threeIv.isSelected = false
                    binding.fourIv.isSelected = false
                    binding.fiveIv.isSelected = false
                    binding.sixIv.isSelected = false
                    binding.sevenIv.isSelected = false

                    binding.oneFirstView.isSelected = true
                    binding.twoFirstView.isSelected = true
                    binding.threeFirstView.isSelected = false
                    binding.fourFirstView.isSelected = false
                    binding.fiveFirstView.isSelected = false
                    binding.sixFirstView.isSelected = false
                    binding.sevenFirstView.isSelected = false

                    binding.oneSecondView.isSelected = true
                    binding.twoSecondView.isSelected = false
                    binding.threeSecondView.isSelected = false
                    binding.fourSecondView.isSelected = false
                    binding.fiveSecondView.isSelected = false
                    binding.sixSecondView.isSelected = false
                    binding.sevenSecondView.isSelected = false

                    binding.oneSign.visibility = View.VISIBLE
                    binding.twoSign.visibility = View.VISIBLE
                    binding.threeSign.visibility = View.INVISIBLE
                    binding.fourSign.visibility = View.INVISIBLE
                    binding.fiveSign.visibility = View.INVISIBLE
                    binding.sixSign.visibility = View.INVISIBLE
                    binding.sevenSign.visibility = View.INVISIBLE
                }

                3 -> {
                    binding.oneIv.isSelected = true
                    binding.twoIv.isSelected = true
                    binding.threeIv.isSelected = true
                    binding.fourIv.isSelected = false
                    binding.fiveIv.isSelected = false
                    binding.sixIv.isSelected = false
                    binding.sevenIv.isSelected = false

                    binding.oneFirstView.isSelected = true
                    binding.twoFirstView.isSelected = true
                    binding.threeFirstView.isSelected = true
                    binding.fourFirstView.isSelected = false
                    binding.fiveFirstView.isSelected = false
                    binding.sixFirstView.isSelected = false
                    binding.sevenFirstView.isSelected = false

                    binding.oneSecondView.isSelected = true
                    binding.twoSecondView.isSelected = true
                    binding.threeSecondView.isSelected = false
                    binding.fourSecondView.isSelected = false
                    binding.fiveSecondView.isSelected = false
                    binding.sixSecondView.isSelected = false
                    binding.sevenSecondView.isSelected = false

                    binding.oneSign.visibility = View.VISIBLE
                    binding.twoSign.visibility = View.VISIBLE
                    binding.threeSign.visibility = View.VISIBLE
                    binding.fourSign.visibility = View.INVISIBLE
                    binding.fiveSign.visibility = View.INVISIBLE
                    binding.sixSign.visibility = View.INVISIBLE
                    binding.sevenSign.visibility = View.INVISIBLE
                }

                4 -> {
                    binding.oneIv.isSelected = true
                    binding.twoIv.isSelected = true
                    binding.threeIv.isSelected = true
                    binding.fourIv.isSelected = true
                    binding.fiveIv.isSelected = false
                    binding.sixIv.isSelected = false
                    binding.sevenIv.isSelected = false

                    binding.oneFirstView.isSelected = true
                    binding.twoFirstView.isSelected = true
                    binding.threeFirstView.isSelected = true
                    binding.fourFirstView.isSelected = true
                    binding.fiveFirstView.isSelected = false
                    binding.sixFirstView.isSelected = false
                    binding.sevenFirstView.isSelected = false

                    binding.oneSecondView.isSelected = true
                    binding.twoSecondView.isSelected = true
                    binding.threeSecondView.isSelected = true
                    binding.fourSecondView.isSelected = false
                    binding.fiveSecondView.isSelected = false
                    binding.sixSecondView.isSelected = false
                    binding.sevenSecondView.isSelected = false

                    binding.oneSign.visibility = View.VISIBLE
                    binding.twoSign.visibility = View.VISIBLE
                    binding.threeSign.visibility = View.VISIBLE
                    binding.fourSign.visibility = View.VISIBLE
                    binding.fiveSign.visibility = View.INVISIBLE
                    binding.sixSign.visibility = View.INVISIBLE
                    binding.sevenSign.visibility = View.INVISIBLE
                }

                5 -> {
                    binding.oneIv.isSelected = true
                    binding.twoIv.isSelected = true
                    binding.threeIv.isSelected = true
                    binding.fourIv.isSelected = true
                    binding.fiveIv.isSelected = true
                    binding.sixIv.isSelected = false
                    binding.sevenIv.isSelected = false

                    binding.oneFirstView.isSelected = true
                    binding.twoFirstView.isSelected = true
                    binding.threeFirstView.isSelected = true
                    binding.fourFirstView.isSelected = true
                    binding.fiveFirstView.isSelected = true
                    binding.sixFirstView.isSelected = false
                    binding.sevenFirstView.isSelected = false

                    binding.oneSecondView.isSelected = true
                    binding.twoSecondView.isSelected = true
                    binding.threeSecondView.isSelected = true
                    binding.fourSecondView.isSelected = true
                    binding.fiveSecondView.isSelected = false
                    binding.sixSecondView.isSelected = false
                    binding.sevenSecondView.isSelected = false

                    binding.oneSign.visibility = View.VISIBLE
                    binding.twoSign.visibility = View.VISIBLE
                    binding.threeSign.visibility = View.VISIBLE
                    binding.fourSign.visibility = View.VISIBLE
                    binding.fiveSign.visibility = View.VISIBLE
                    binding.sixSign.visibility = View.INVISIBLE
                    binding.sevenSign.visibility = View.INVISIBLE
                }

                6 -> {
                    binding.oneIv.isSelected = true
                    binding.twoIv.isSelected = true
                    binding.threeIv.isSelected = true
                    binding.fourIv.isSelected = true
                    binding.fiveIv.isSelected = true
                    binding.sixIv.isSelected = true
                    binding.sevenIv.isSelected = false

                    binding.oneFirstView.isSelected = true
                    binding.twoFirstView.isSelected = true
                    binding.threeFirstView.isSelected = true
                    binding.fourFirstView.isSelected = true
                    binding.fiveFirstView.isSelected = true
                    binding.sixFirstView.isSelected = true
                    binding.sevenFirstView.isSelected = false

                    binding.oneSecondView.isSelected = true
                    binding.twoSecondView.isSelected = true
                    binding.threeSecondView.isSelected = true
                    binding.fourSecondView.isSelected = true
                    binding.fiveSecondView.isSelected = true
                    binding.sixSecondView.isSelected = false
                    binding.sevenSecondView.isSelected = false

                    binding.oneSign.visibility = View.VISIBLE
                    binding.twoSign.visibility = View.VISIBLE
                    binding.threeSign.visibility = View.VISIBLE
                    binding.fourSign.visibility = View.VISIBLE
                    binding.fiveSign.visibility = View.VISIBLE
                    binding.sixSign.visibility = View.VISIBLE
                    binding.sevenSign.visibility = View.INVISIBLE
                }

                7 -> {
                    binding.oneIv.isSelected = true
                    binding.twoIv.isSelected = true
                    binding.threeIv.isSelected = true
                    binding.fourIv.isSelected = true
                    binding.fiveIv.isSelected = true
                    binding.sixIv.isSelected = true
                    binding.sevenIv.isSelected = true

                    binding.oneFirstView.isSelected = true
                    binding.twoFirstView.isSelected = true
                    binding.threeFirstView.isSelected = true
                    binding.fourFirstView.isSelected = true
                    binding.fiveFirstView.isSelected = true
                    binding.sixFirstView.isSelected = true
                    binding.sevenFirstView.isSelected = true

                    binding.oneSecondView.isSelected = true
                    binding.twoSecondView.isSelected = true
                    binding.threeSecondView.isSelected = true
                    binding.fourSecondView.isSelected = true
                    binding.fiveSecondView.isSelected = true
                    binding.sixSecondView.isSelected = true
                    binding.sevenSecondView.isSelected = true

                    binding.oneSign.visibility = View.VISIBLE
                    binding.twoSign.visibility = View.VISIBLE
                    binding.threeSign.visibility = View.VISIBLE
                    binding.fourSign.visibility = View.VISIBLE
                    binding.fiveSign.visibility = View.VISIBLE
                    binding.sixSign.visibility = View.VISIBLE
                    binding.sevenSign.visibility = View.VISIBLE
                }

                else -> {
                    if (it > 7) {
                        binding.oneIv.isSelected = true
                        binding.twoIv.isSelected = true
                        binding.threeIv.isSelected = true
                        binding.fourIv.isSelected = true
                        binding.fiveIv.isSelected = true
                        binding.sixIv.isSelected = true
                        binding.sevenIv.isSelected = true

                        binding.oneFirstView.isSelected = true
                        binding.twoFirstView.isSelected = true
                        binding.threeFirstView.isSelected = true
                        binding.fourFirstView.isSelected = true
                        binding.fiveFirstView.isSelected = true
                        binding.sixFirstView.isSelected = true
                        binding.sevenFirstView.isSelected = true

                        binding.oneSecondView.isSelected = true
                        binding.twoSecondView.isSelected = true
                        binding.threeSecondView.isSelected = true
                        binding.fourSecondView.isSelected = true
                        binding.fiveSecondView.isSelected = true
                        binding.sixSecondView.isSelected = true
                        binding.sevenSecondView.isSelected = true

                        binding.oneSign.visibility = View.VISIBLE
                        binding.twoSign.visibility = View.VISIBLE
                        binding.threeSign.visibility = View.VISIBLE
                        binding.fourSign.visibility = View.VISIBLE
                        binding.fiveSign.visibility = View.VISIBLE
                        binding.sixSign.visibility = View.VISIBLE
                        binding.sevenSign.visibility = View.VISIBLE
                    }

                }
            }
        })
    }
}